

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc]关于创建型设计模式在源码中的应用的笔记 笔记1：设计模式 笔记1 | 五个创建型模式 | Java 语言实现 | 工厂方法设计模式 | 抽象工厂模式 |建造者模式 | 单例模式 | 原型模式_程序喵 尤Ni的博客-CSDN博客 一、创建型设计模式 知识回顾创建型设计模式 主要有五种，分别是：  工厂方法设计模式 抽象工厂模式 建造者模式 单例模式 原型模式  接下来用简要的一句话概括这">
<meta property="og:type" content="article">
<meta property="og:title" content="设计模式 笔记11 _ 创建型设计模式在源码中的应用 总结篇（一）">
<meta property="og:url" content="http://example.com/2022/07/29/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20%E7%AC%94%E8%AE%B011%20_%20%E5%88%9B%E5%BB%BA%E5%9E%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E5%9C%A8%E6%BA%90%E7%A0%81%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8%20%E6%80%BB%E7%BB%93%E7%AF%87%EF%BC%88%E4%B8%80%EF%BC%89/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc]关于创建型设计模式在源码中的应用的笔记 笔记1：设计模式 笔记1 | 五个创建型模式 | Java 语言实现 | 工厂方法设计模式 | 抽象工厂模式 |建造者模式 | 单例模式 | 原型模式_程序喵 尤Ni的博客-CSDN博客 一、创建型设计模式 知识回顾创建型设计模式 主要有五种，分别是：  工厂方法设计模式 抽象工厂模式 建造者模式 单例模式 原型模式  接下来用简要的一句话概括这">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/3cea605298af47cb96f25d613e23d110.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ec50f8c9198a4978830642f6f109f3b8.gif#pic_center">
<meta property="article:published_time" content="2022-07-29T09:25:31.000Z">
<meta property="article:modified_time" content="2022-08-22T15:41:03.052Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="设计模式">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/3cea605298af47cb96f25d613e23d110.png">
  
  
  
  <title>设计模式 笔记11 _ 创建型设计模式在源码中的应用 总结篇（一） - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="设计模式 笔记11 _ 创建型设计模式在源码中的应用 总结篇（一）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-29 17:25" pubdate>
          2022年7月29日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          12k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          104 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">设计模式 笔记11 _ 创建型设计模式在源码中的应用 总结篇（一）</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]<br>关于创建型设计模式在源码中的应用的笔记</p>
<p>笔记1：<a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/126048088">设计模式 笔记1 | 五个创建型模式 | Java 语言实现 | 工厂方法设计模式 | 抽象工厂模式 |建造者模式 | 单例模式 | 原型模式_程序喵 尤Ni的博客-CSDN博客</a></p>
<h1 id="一、创建型设计模式-知识回顾"><a href="#一、创建型设计模式-知识回顾" class="headerlink" title="一、创建型设计模式 知识回顾"></a>一、创建型设计模式 知识回顾</h1><p>创建型设计模式 主要有五种，分别是：</p>
<ul>
<li>工厂方法设计模式</li>
<li>抽象工厂模式</li>
<li>建造者模式</li>
<li>单例模式</li>
<li>原型模式</li>
</ul>
<p>接下来用简要的一句话概括这些设计模式的定义和主要组成部分：</p>
<h2 id="1-1-工厂方法设计模式"><a href="#1-1-工厂方法设计模式" class="headerlink" title="1.1 工厂方法设计模式"></a>1.1 工厂方法设计模式</h2><p>定义：提供符合开闭原则的工厂对象，支持获取同一类型的产品。<br>特点：工厂和产品都有抽象和具体实现两种。<br>主要组成：<br>![](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/img_convert/8ac8b2ad42a7abdaa4884f4043603ac0.jpeg#pic_center">https://img-blog.csdnimg.cn/img_convert/8ac8b2ad42a7abdaa4884f4043603ac0.jpeg#pic_center</a> &#x3D;80%x)</p>
<h2 id="1-2-抽象工厂模式"><a href="#1-2-抽象工厂模式" class="headerlink" title="1.2 抽象工厂模式"></a>1.2 抽象工厂模式</h2><p>定义：为访问类提供一组相关的接口，支持获取多种类型的产品。<br>特点：工厂方法的升级版，符合开闭原则。<br>主要组成：<br>![](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/img_convert/6a5dff22a32689d06b298a1f26352440.jpeg#pic_center">https://img-blog.csdnimg.cn/img_convert/6a5dff22a32689d06b298a1f26352440.jpeg#pic_center</a> &#x3D;80%x)</p>
<h2 id="1-3-建造者模式"><a href="#1-3-建造者模式" class="headerlink" title="1.3 建造者模式"></a>1.3 建造者模式</h2><p>定义：将创建对象的过程根据其属性分成多个步骤，每一个步骤支持单独处理部分的属性。<br>特点：可以和工厂方法模式结合使用，封装性好<br>主要组成：<br>![](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/img_convert/959811489e41982e9fced7f9ad30db46.jpeg#pic_center">https://img-blog.csdnimg.cn/img_convert/959811489e41982e9fced7f9ad30db46.jpeg#pic_center</a> &#x3D;80%x)<br>其中抽象建造者和具体建造者可以合并，直接放在产品角色中，即静态内部类的方式实现建造者模式，这种方式比较常用，其组成图如下：<br>![](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/img_convert/4e26128d3b5ed646b4db8d1ae85f912e.jpeg#pic_center">https://img-blog.csdnimg.cn/img_convert/4e26128d3b5ed646b4db8d1ae85f912e.jpeg#pic_center</a> &#x3D;80%x)</p>
<h2 id="1-4-单例模式"><a href="#1-4-单例模式" class="headerlink" title="1.4 单例模式"></a>1.4 单例模式</h2><p>定义：只有一个单例对象，通常使用 static 关键字实现<br>特点：分为懒汉式 和 饿汉式两种，前者在多线程环境下需要用到 synchronized 同步，防止多线程抢占资源<br>主要组成：只有一个单例对象，贴一个不使用 synchronized ，而用 Java类加载机制完美实现懒汉式的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Singleton</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-title function_">Singleton</span><span class="hljs-params">()</span>&#123;&#125; <span class="hljs-comment">// 私有化无参构造</span><br><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Holder</span>&#123; <span class="hljs-comment">// 静态内部类</span><br>        <span class="hljs-comment">// 根据类加载特性, 仅使用Singleton类时，不会对静态内部类进行初始化</span><br>        <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">static</span> <span class="hljs-type">Singleton</span> <span class="hljs-variable">INSTANCE</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Singleton</span>();<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Singleton <span class="hljs-title function_">getInstance</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> Holder.INSTANCE; <span class="hljs-comment">// 直接获取内部类的静态实例</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="1-5-原型模式"><a href="#1-5-原型模式" class="headerlink" title="1.5 原型模式"></a>1.5 原型模式</h2><p>定义：用已创建的对象实例作为原型，通过复制该原型对象来创建一个和原型相同货类似的新对象。<br>特点：分为浅克隆和深克隆两种，Object类的clone() 属于 浅克隆，比直接new快<br>主要组成：<br>![](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/img_convert/4e8b6267b5826eefdee38a00a15cfd55.jpeg#pic_center">https://img-blog.csdnimg.cn/img_convert/4e8b6267b5826eefdee38a00a15cfd55.jpeg#pic_center</a> &#x3D;30%x)</p>
<h1 id="二、创建型设计模式在源码中的应用总结"><a href="#二、创建型设计模式在源码中的应用总结" class="headerlink" title="二、创建型设计模式在源码中的应用总结"></a>二、创建型设计模式在源码中的应用总结</h1><h2 id="2-1-工厂方法模式-logback"><a href="#2-1-工厂方法模式-logback" class="headerlink" title="2.1 工厂方法模式 logback"></a>2.1 工厂方法模式 logback</h2><p>参考笔记：<a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/125911608">https://blog.csdn.net/Unirithe/article/details/125911608</a><br><a target="_blank" rel="noopener" href="https://logback.qos.ch/manual/"><strong>logback</strong></a>** **是一种日志管理框架，由 log4j （流行的日志管理框架）创始人推出，是 log4j 的升级版，同时也是 SpringBoot 默认的日志管理框架</p>
<p>日志级别分类以及优先级为：ERROR &gt; WARN &gt; INFO &gt; DEBUG &gt;TRACE</p>
<p><strong>logback 架构：</strong></p>
<p>![](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/img_convert/df8e3b69321a6fc3d8a3fb844b2ab3d4.jpeg#pic_center">https://img-blog.csdnimg.cn/img_convert/df8e3b69321a6fc3d8a3fb844b2ab3d4.jpeg#pic_center</a> &#x3D;80%x)</p>
<ul>
<li>logback-core，框架代码核心，其他两个模块依赖此模块</li>
<li>logback-classic，log4j优化版本，实现了原生log4j</li>
<li>logback-access，可集成到现有的Servlet容器，如Tomcat、Jetty（按需引入）</li>
</ul>
<p><strong>主要的三个接口：</strong></p>
<ul>
<li>Logger，属于 logback-classic 模块，最常用的接口，用于获取日志对象</li>
<li>Appender 和 Layout 接口，属于 logback-core 模块</li>
</ul>
<p>使用到工厂模式的类：<code>LoggerFactory.java</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">LoggerFactory</span> &#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-title function_">LoggerFactory</span><span class="hljs-params">()</span> &#123;&#125;		<span class="hljs-comment">// 构造方法私有化</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Logger <span class="hljs-title function_">getLogger</span><span class="hljs-params">(Class&lt;?&gt; clazz)</span> &#123;<br>        ...<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>LoggerFactory 是 logback-classic 提供的 日志工厂类，我们一般是通过这个工厂里的方法来获取日志对象 Logger 的。<br>getLogger 是 日志工厂提供的静态方法，是重载的方法，最常见的有 String 和 Class两个参数：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Logger <span class="hljs-title function_">getLogger</span><span class="hljs-params">(Class&lt;?&gt; clazz)</span> &#123;<br>    <span class="hljs-comment">// 获取字节码的名称，调用重载方法获取 Logger</span><br>    <span class="hljs-type">Logger</span> <span class="hljs-variable">logger</span> <span class="hljs-operator">=</span> getLogger(clazz.getName());<br>    ...<br>    <span class="hljs-keyword">return</span> logger;<br>&#125;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Logger <span class="hljs-title function_">getLogger</span><span class="hljs-params">(String name)</span> &#123;<br>    <span class="hljs-comment">// 获取工厂实现类</span><br>    <span class="hljs-type">ILoggerFactory</span> <span class="hljs-variable">iLoggerFactory</span> <span class="hljs-operator">=</span> getILoggerFactory();<br>    <span class="hljs-keyword">return</span> iLoggerFactory.getLogger(name);<br>&#125;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ILoggerFactory <span class="hljs-title function_">getILoggerFactory</span><span class="hljs-params">()</span> &#123;...&#125;<br></code></pre></td></tr></table></figure>

<p>通过源码可以看出，getLogger 是日志工厂里可以获取日志的方法，，其本质吊用了日志工厂对应的抽象工厂，即 iLoggerFactory 接口的 getLogger方法。</p>
<h2 id="2-2-抽象工厂模式-Spring"><a href="#2-2-抽象工厂模式-Spring" class="headerlink" title="2.2 抽象工厂模式 Spring"></a>2.2 抽象工厂模式 Spring</h2><p>参考笔记：</p>
<p>Spring 是 Java EE 编程领域的一个轻量级开源框架，集成了各类型的工具，通过核心的 BeanFactory 实现了底层的类的实例化和生命周期的管理</p>
<p>Spring特点：IOC 控制反转，DI 依赖注入，AOP 面向切面编程</p>
<p>Spring中的抽象工厂模式：</p>
<p>BeanFactory 接口声明了一系列关于获取 Bean 的方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> org.springframework.beans.factory;<br><br><span class="hljs-keyword">import</span> org.springframework.beans.BeansException;<br><span class="hljs-keyword">import</span> org.springframework.core.ResolvableType;<br><span class="hljs-keyword">import</span> org.springframework.lang.Nullable;<br><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">interface</span> <span class="hljs-title class_">BeanFactory</span> &#123;<br><br>    <span class="hljs-comment">// Bean 自带的前缀</span><br>	<span class="hljs-type">String</span> <span class="hljs-variable">FACTORY_BEAN_PREFIX</span> <span class="hljs-operator">=</span> <span class="hljs-string">&quot;&amp;&quot;</span>;<br>    <span class="hljs-comment">// 1. 根据名称获取 Bean</span><br>	Object <span class="hljs-title function_">getBean</span><span class="hljs-params">(String name)</span> <span class="hljs-keyword">throws</span> BeansException;<br>    <span class="hljs-comment">// 2. 根据名称获取 Bean, 并转化为指定的对象</span><br>	&lt;T&gt; T <span class="hljs-title function_">getBean</span><span class="hljs-params">(String name, Class&lt;T&gt; requiredType)</span> <span class="hljs-keyword">throws</span> BeansException;<br>    <span class="hljs-comment">// 3. 根据名称获取 Bean, 并传入构造该Bean的参数，用于有参构造</span><br>	Object <span class="hljs-title function_">getBean</span><span class="hljs-params">(String name, Object... args)</span> <span class="hljs-keyword">throws</span> BeansException;<br>    <span class="hljs-comment">// 4. 根据字节码获取 Bean</span><br>	&lt;T&gt; T <span class="hljs-title function_">getBean</span><span class="hljs-params">(Class&lt;T&gt; requiredType)</span> <span class="hljs-keyword">throws</span> BeansException;<br>    <span class="hljs-comment">// 5. 根据字节码获取 Bean，并传入构造该Bean的参数，用于有参构造</span><br>	&lt;T&gt; T <span class="hljs-title function_">getBean</span><span class="hljs-params">(Class&lt;T&gt; requiredType, Object... args)</span> <span class="hljs-keyword">throws</span> BeansException;<br>    <span class="hljs-comment">// 6. 根据字节码获取指定 Bean 的提供者</span><br>	&lt;T&gt; ObjectProvider&lt;T&gt; <span class="hljs-title function_">getBeanProvider</span><span class="hljs-params">(Class&lt;T&gt; requiredType)</span>;<br>    <span class="hljs-comment">// 7. 根据指定的解析类型获取 Bean 的提供者</span><br>	&lt;T&gt; ObjectProvider&lt;T&gt; <span class="hljs-title function_">getBeanProvider</span><span class="hljs-params">(ResolvableType requiredType)</span>;<br>    <span class="hljs-comment">// 8. 判断工厂是否包含 Bean</span><br>	<span class="hljs-type">boolean</span> <span class="hljs-title function_">containsBean</span><span class="hljs-params">(String name)</span>;<br>    <span class="hljs-comment">// 9. 根据名称判断 Bean 是否为单例的对象</span><br>	<span class="hljs-type">boolean</span> <span class="hljs-title function_">isSingleton</span><span class="hljs-params">(String name)</span> <span class="hljs-keyword">throws</span> NoSuchBeanDefinitionException;<br>    <span class="hljs-comment">// 10. 根据名称判断 Bean 是否为原型对象</span><br>	<span class="hljs-type">boolean</span> <span class="hljs-title function_">isPrototype</span><span class="hljs-params">(String name)</span> <span class="hljs-keyword">throws</span> NoSuchBeanDefinitionException;<br>    <span class="hljs-comment">// 11. 根据名称判断 Bean 是否能被指定的可解析类型解析</span><br>	<span class="hljs-type">boolean</span> <span class="hljs-title function_">isTypeMatch</span><span class="hljs-params">(String name, ResolvableType typeToMatch)</span> <span class="hljs-keyword">throws</span> NoSuchBeanDefinitionException;<br>    <span class="hljs-comment">// 12. 根据名称判断 Bean 是否被指定的字节码对应的类进行解析</span><br>	<span class="hljs-type">boolean</span> <span class="hljs-title function_">isTypeMatch</span><span class="hljs-params">(String name, Class&lt;?&gt; typeToMatch)</span> <span class="hljs-keyword">throws</span> NoSuchBeanDefinitionException;<br>    <span class="hljs-comment">// 13. 根据名称获取 Bean 的类型对应的字节码</span><br>	<span class="hljs-meta">@Nullable</span><br>	Class&lt;?&gt; getType(String name) <span class="hljs-keyword">throws</span> NoSuchBeanDefinitionException;<br>    <span class="hljs-comment">// 14. 根据名称获取 Bean 的类型对应的字节码，并设置是否允许Bean初始化</span><br>	<span class="hljs-meta">@Nullable</span><br>	Class&lt;?&gt; getType(String name, <span class="hljs-type">boolean</span> allowFactoryBeanInit) <span class="hljs-keyword">throws</span> NoSuchBeanDefinitionException;<br>    <span class="hljs-comment">// 15. 获取 Bean的别名</span><br>	String[] getAliases(String name);<br>&#125;<br><br></code></pre></td></tr></table></figure>

<p>Spring 提供的 BeanFacory接口，具有多个抽象的实现类，可视为抽象工厂：<br><img src="https://img-blog.csdnimg.cn/3cea605298af47cb96f25d613e23d110.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>其中比较眼熟的就是 AbstractApplicatoinContext，我们获取 Spring 中容器里的 Bean时就是通过这个应用上下文的实现类，其定义为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">AbstractApplicationContext</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">DefaultResourceLoader</span><br>		<span class="hljs-keyword">implements</span> <span class="hljs-title class_">ConfigurableApplicationContext</span> &#123;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>传统方式获取 Spring 容器中的 Bean：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">ApplicationContext</span> <span class="hljs-variable">context</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ClassPathXmlApplicationContext</span>(<span class="hljs-string">&quot;applicationContext.xml&quot;</span>);<br><span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> (User) context.getBean(User.class);<br></code></pre></td></tr></table></figure>
<p>其中 ClassPathXmlApplicationContext 类就是之前 AbstractApplicationContext 的实现子类，不过并不是直接的实现子类<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/58543f3df8b540a9b0e53dc29c755027.png#">https://img-blog.csdnimg.cn/58543f3df8b540a9b0e53dc29c755027.png#</a> &#x3D;60%x)</p>
<p>而 ApplicationContext 其实就是 BeanFactory 的子接口</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/6954b198b8d947318431cb46738d21f2.png#">https://img-blog.csdnimg.cn/6954b198b8d947318431cb46738d21f2.png#</a> &#x3D;80%x)</p>
<p>所以之前获取 Bean 的方法getBean(User.class)，本质上就是调用了 BeanFactory 接口声明的方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> (User) context.getBean(User.class);<br></code></pre></td></tr></table></figure>

<p>总结：</p>
<p>Spring 底层定义了 BeanFactory 工厂接口，用于声明关于获取 Bean 的方法，该接口有若干个 AbastractBeanFactory 抽象工厂的子类，包括AbstractApplicationContext 子类，这个子类实现了 ApplicationContext接口， 而后者这个接口就是 BeanFactory 的子接口，即 AbstractApplicationContext间接实现了 BeanFactory 接口。</p>
<p>以上的设计模式是标准的抽象工厂方法的设计模式。</p>
<h2 id="2-3-建造者模式在源码中的应用-StringBuilder-Mybatis"><a href="#2-3-建造者模式在源码中的应用-StringBuilder-Mybatis" class="headerlink" title="2.3 建造者模式在源码中的应用 StringBuilder + Mybatis"></a>2.3 建造者模式在源码中的应用 StringBuilder + Mybatis</h2><h3 id="2-3-1-StringBuilder-建造者模式"><a href="#2-3-1-StringBuilder-建造者模式" class="headerlink" title="2.3.1  StringBuilder 建造者模式"></a>2.3.1  StringBuilder 建造者模式</h3><p><code>java.lang.StringBuilder</code>是 JDK 源码提供的支持 String 类型一系列操作的类</p>
<p>在创建 StringBuilder() 对象时，可以使用 append 方法添加字符串内容，也可以做创建后调用 append() ，例如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">String</span> <span class="hljs-variable">s</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuilder</span>()<br>    .append(<span class="hljs-string">&quot;hello, &quot;</span>)<br>    .append(<span class="hljs-string">&quot;Wolrd!&quot;</span>)<br>    .toString();<br></code></pre></td></tr></table></figure>
<p>toString() 可视为建造者的 build () 方法<br><strong>StringBuilder 拓展：</strong> 部分源码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">StringBuilder</span><br>    <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AbstractStringBuilder</span><br>    <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Serializable</span>, CharSequence<br>&#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> StringBuilder <span class="hljs-title function_">append</span><span class="hljs-params">(Object obj)</span> &#123;<br>        <span class="hljs-keyword">return</span> append(String.valueOf(obj));<br>    &#125;<br>    ...<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">toString</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-comment">// 创建拷贝对象, 不分享内部的字符数组</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(value, <span class="hljs-number">0</span>, count);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>StringBuilder 内部类存储字符串的变量为 <code>char[] value</code><br>这个字符串数组是存在其父类 <code>AbstactStringBuilder</code>中的，在 StringBuilder 的无参构造中，初始化该字符数组的长度为 16</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-title function_">StringBuilder</span><span class="hljs-params">()</span> &#123;<br>    <span class="hljs-built_in">super</span>(<span class="hljs-number">16</span>);<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="2-3-2-StringBuilder-扩容机制"><a href="#2-3-2-StringBuilder-扩容机制" class="headerlink" title="2.3.2 StringBuilder 扩容机制"></a>2.3.2 StringBuilder 扩容机制</h3><p>SpringBuilder 是有扩容机制的，初始化的字符长度为16，当长度超过这个值时，则会调用 ensureCapacityInternal() 方法来进行扩容，这个方法是在其抽象父类 AbstractStringBuilder中实现的：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">ensureCapacityInternal</span><span class="hljs-params">(<span class="hljs-type">int</span> minimumCapacity)</span> &#123;<br>    <span class="hljs-comment">// overflow-conscious code</span><br>    <span class="hljs-keyword">if</span> (minimumCapacity - value.length &gt; <span class="hljs-number">0</span>) &#123;<br>        value = Arrays.copyOf(value,<br>                              newCapacity(minimumCapacity));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>接下来可以通过调试观察其扩容过程：<br>测试代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">String</span> <span class="hljs-variable">s</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">StringBuilder</span>()<br>    .append(<span class="hljs-string">&quot;1234567890&quot;</span>)<br>    .append(<span class="hljs-string">&quot;1234567&quot;</span>)<br>    .toString();<br></code></pre></td></tr></table></figure>
<p>调试过程：</p>
<p><img src="https://img-blog.csdnimg.cn/ec50f8c9198a4978830642f6f109f3b8.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h3 id="2-3-2-MyBatis-建造者模式"><a href="#2-3-2-MyBatis-建造者模式" class="headerlink" title="2.3.2 MyBatis 建造者模式"></a>2.3.2 MyBatis 建造者模式</h3><p>Mybatis 提供了 SqlSessionFactoryBuilder 类用于创建 SqlSessionFactory，即创建 Sql 会话工厂对象。这里是建造者模式和工厂模式的综合使用</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SqlSessionFactoryBuilder</span> &#123;<br>    ...<br>    <span class="hljs-keyword">public</span> SqlSessionFactory <span class="hljs-title function_">build</span><span class="hljs-params">(Configuration config)</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">DefaultSqlSessionFactory</span>(config);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>build () 方法根据 Configuration 配置对象创建一个默认的工厂对象。</p>
<p>在 SqlSessionFactoryBuilder 类中有几个重载的 build 方法，其中主要的实现为：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">SqlSessionFactoryBuilder</span> &#123;<br>    <br>    <span class="hljs-keyword">public</span> SqlSessionFactory <span class="hljs-title function_">build</span><span class="hljs-params">(InputStream inputStream, String environment, Properties properties)</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 创建建造者 XMLConfigBuilder的 实例</span><br>            <span class="hljs-type">XMLConfigBuilder</span> <span class="hljs-variable">parser</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">XMLConfigBuilder</span>(inputStream, environment, properties);<br>            <span class="hljs-comment">// XMLConfigBuilder 的 parse() 创建 Configuration 实例</span><br>            <span class="hljs-keyword">return</span> build(parser.parse());<br>        &#125; <span class="hljs-keyword">catch</span> (Exception e) &#123;<br>            <span class="hljs-comment">// 捕获异常</span><br>            <span class="hljs-keyword">throw</span> ExceptionFactory.wrapException(<span class="hljs-string">&quot;Error building SqlSession.&quot;</span>, e);<br>        &#125; <span class="hljs-keyword">finally</span> &#123;<br>            <span class="hljs-comment">// 重置错误</span><br>            ErrorContext.instance().reset();<br>            <span class="hljs-keyword">try</span> &#123;<br>                <span class="hljs-comment">// 释放资源</span><br>                inputStream.close();<br>            &#125; <span class="hljs-keyword">catch</span> (IOException e) &#123;<br>                <span class="hljs-comment">// IO 错误不进行处理</span><br>            &#125;<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>这里的 XMLConfigurBuilder 是用于创建 Configuration 的建造者，可以视为建造者模式中的具体建造者。</p>
<h2 id="2-4-单例模式在源码中的应用"><a href="#2-4-单例模式在源码中的应用" class="headerlink" title="2.4 单例模式在源码中的应用"></a>2.4 单例模式在源码中的应用</h2><h3 id="2-4-1-Runtime"><a href="#2-4-1-Runtime" class="headerlink" title="2.4.1  Runtime"></a>2.4.1  Runtime</h3><p><code>java.lang.Runtime</code>是从 JDK 1.0 开始出现的，用于启动应用程序的类<br>【例1】使用 Runtime 打开系统的记事本</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">Runtime.getRumtime().exec(<span class="hljs-string">&quot;notepad&quot;</span>);<br></code></pre></td></tr></table></figure>
<p>【例2】使用 Runtime 实现 10秒后 自动关机</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java">Runtime.getRuntime().exec(<span class="hljs-string">&quot;shut down -s -t 10&quot;</span>)<br></code></pre></td></tr></table></figure>
<p>在 Runtime 源码实现中，使用了 饿汉式的单例模式，部分源码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Runtime</span>&#123;<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-type">Runtime</span> <span class="hljs-variable">currentRuntime</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Runtime</span>();<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Runtime <span class="hljs-title function_">getRuntime</span><span class="hljs-params">()</span>&#123;<br>         <span class="hljs-keyword">return</span> currentRuntime;   <br>    &#125;<br>    <span class="hljs-keyword">private</span> <span class="hljs-title function_">Rumtime</span><span class="hljs-params">()</span>&#123;&#125;<br>    ...<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="2-4-2-Integer-基本类型的引用类型"><a href="#2-4-2-Integer-基本类型的引用类型" class="headerlink" title="2.4.2 Integer 基本类型的引用类型"></a>2.4.2 Integer 基本类型的引用类型</h3><p>Java 提供了 boolean、byte、int 、float、double、char[] 等基本类型对应的引用类型，分别是 Boolean、Byte、Integer、Float、Double、String，本次只用了解 Integer</p>
<h4 id="1-自动拆箱与自动装箱"><a href="#1-自动拆箱与自动装箱" class="headerlink" title="1. 自动拆箱与自动装箱"></a>1. 自动拆箱与自动装箱</h4><p>JDK 8 中 Integer 是 int 类型的引用类型，它们之间可以互相转换，这个过程称作自动装箱和自动拆箱，例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Demo</span>&#123;<br>     <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>         <span class="hljs-type">Integer</span> <span class="hljs-variable">a</span> <span class="hljs-operator">=</span> <span class="hljs-number">10</span>; <br>         <span class="hljs-type">int</span> <span class="hljs-variable">b</span> <span class="hljs-operator">=</span> a;<br>         System.out.println(a + <span class="hljs-string">&quot;=&quot;</span> + b);<br>     &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>Integer -&gt;int 称为 自动拆箱，本质上调用的是 Integer 的 intValue() 方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-type">int</span> <span class="hljs-title function_">intValue</span><span class="hljs-params">()</span> &#123;<br>    <span class="hljs-keyword">return</span> value;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>int -&gt; Integer 称为自动装箱，本质上调用的是 Integer 的 valueOf () 方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Integer <span class="hljs-title function_">valueOf</span><span class="hljs-params">(<span class="hljs-type">int</span> i)</span> &#123;<br>    <span class="hljs-keyword">if</span> (i &gt;= IntegerCache.low &amp;&amp; i &lt;= IntegerCache.high)<br>        <span class="hljs-keyword">return</span> IntegerCache.cache[i + (-IntegerCache.low)];<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Integer</span>(i);<br>&#125;<br></code></pre></td></tr></table></figure>
<h4 id="2-Integer-内部缓存数据"><a href="#2-Integer-内部缓存数据" class="headerlink" title="2. Integer 内部缓存数据"></a>2. Integer 内部缓存数据</h4><p>Integer 默认提供了 [ -128 , 127 ] 数据之间的 IntegerCache 缓存数组<br>当我们定义的 Integer 型变量属于这个范围时，valueOf () 方法会直接返回 IntegerCache数组里的 Integer 对象</p>
<h4 id="3-IntegerCache-饿汉式"><a href="#3-IntegerCache-饿汉式" class="headerlink" title="3. IntegerCache 饿汉式"></a>3. IntegerCache 饿汉式</h4><p>IntegerCache是 Integer 的静态内部类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> java.lang;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Integer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Number</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">Comparable</span>&lt;Integer&gt; &#123;<br>    ...<br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">IntegerCache</span> &#123;<br>            <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> <span class="hljs-variable">low</span> <span class="hljs-operator">=</span> -<span class="hljs-number">128</span>;<br>            <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-type">int</span> high;<br>            <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Integer cache[];<br>    <br>            <span class="hljs-keyword">static</span> &#123;<br>                <span class="hljs-comment">// high value may be configured by property</span><br>                <span class="hljs-type">int</span> <span class="hljs-variable">h</span> <span class="hljs-operator">=</span> <span class="hljs-number">127</span>;<br>                <span class="hljs-type">String</span> <span class="hljs-variable">integerCacheHighPropValue</span> <span class="hljs-operator">=</span><br>                    sun.misc.VM.getSavedProperty(<span class="hljs-string">&quot;java.lang.Integer.IntegerCache.high&quot;</span>);<br>                <span class="hljs-keyword">if</span> (integerCacheHighPropValue != <span class="hljs-literal">null</span>) &#123;<br>                    <span class="hljs-keyword">try</span> &#123;<br>                        <span class="hljs-type">int</span> <span class="hljs-variable">i</span> <span class="hljs-operator">=</span> parseInt(integerCacheHighPropValue);<br>                        i = Math.max(i, <span class="hljs-number">127</span>);<br>                        <span class="hljs-comment">// Maximum array size is Integer.MAX_VALUE</span><br>                        h = Math.min(i, Integer.MAX_VALUE - (-low) -<span class="hljs-number">1</span>);<br>                    &#125; <span class="hljs-keyword">catch</span>( NumberFormatException nfe) &#123;<br>                        <span class="hljs-comment">// If the property cannot be parsed into an int, ignore it.</span><br>                    &#125;<br>                &#125;<br>                high = h;<br>    <br>                cache = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Integer</span>[(high - low) + <span class="hljs-number">1</span>];<br>                <span class="hljs-type">int</span> <span class="hljs-variable">j</span> <span class="hljs-operator">=</span> low;<br>                <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> <span class="hljs-variable">k</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; k &lt; cache.length; k++)<br>                    cache[k] = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Integer</span>(j++);<br>    <br>                <span class="hljs-comment">// range [-128, 127] must be interned (JLS7 5.1.7)</span><br>                <span class="hljs-keyword">assert</span> IntegerCache.high &gt;= <span class="hljs-number">127</span>;<br>            &#125;<br>    <br>        <span class="hljs-keyword">private</span> <span class="hljs-title function_">IntegerCache</span><span class="hljs-params">()</span> &#123;&#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>通过源码可以看出，IntegerCache 是单例模式中饿汉式的实现，其主要特点有：</p>
<ul>
<li>构造方法私有化，private IntegerCache() {} </li>
<li>内部变量均以 static 修饰</li>
<li>使用 static 代码块 初始化内部的 cache 数组</li>
</ul>
<h3 id="2-4-3-Spring"><a href="#2-4-3-Spring" class="headerlink" title="2.4.3 Spring"></a>2.4.3 Spring</h3><p>Spring 中，Bean 一般有两种定义方式，分别是 prototype（支持多例）和 singleton （单例）</p>
<ul>
<li>prototype，对 bean 的每次请求都会创建一个新的实例</li>
<li>singleton，对 bean 的每次请求都会返回唯一的实例</li>
</ul>
<p>接下来，通过几个例子体会其中的单例模式<br>【例1】在 SpringBoot 中，往 IOC 容器注册单例模式的 Bean</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@SpringBootApplication</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">WebDemoApplication</span> &#123;<br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-meta">@Scope(&quot;singleton&quot;)</span><br>    <span class="hljs-keyword">public</span> String <span class="hljs-title function_">uuid</span><span class="hljs-params">()</span>&#123;<br>        <span class="hljs-keyword">return</span> UUID.randomUUID().toString();<br>    &#125;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-type">ApplicationContext</span> <span class="hljs-variable">context</span> <span class="hljs-operator">=</span> SpringApplication.run(WebDemoApplication.class, args);<br>        <span class="hljs-type">String</span> <span class="hljs-variable">id1</span> <span class="hljs-operator">=</span> context.getBean(String.class);<br>        <span class="hljs-type">String</span> <span class="hljs-variable">id2</span> <span class="hljs-operator">=</span> context.getBean(String.class);<br>        System.out.println(<span class="hljs-string">&quot;id1:&quot;</span> + id1);<br>        System.out.println(<span class="hljs-string">&quot;id2:&quot;</span> + id2);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>运行结果: 输出了相同的id<br>id1:8b58cfd1-ba2c-4759-96d9-fbc44f8a66b5<br>id2:8b58cfd1-ba2c-4759-96d9-fbc44f8a66b5</p>
</blockquote>
<p>【例2】SpringBoot 往 IOC 容器注册多例模式 的 Bean</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Bean</span><br><span class="hljs-meta">@Scope(&quot;prototype&quot;)</span><br><span class="hljs-keyword">public</span> String <span class="hljs-title function_">uuid</span><span class="hljs-params">()</span>&#123;<br>    <span class="hljs-keyword">return</span> UUID.randomUUID().toString();<br>&#125;<br></code></pre></td></tr></table></figure>

<p>Spring 注入 Bean 时默认是 单例模式</p>
<h2 id="2-5-原型模式在源码中的应用"><a href="#2-5-原型模式在源码中的应用" class="headerlink" title="2.5 原型模式在源码中的应用"></a>2.5 原型模式在源码中的应用</h2><h3 id="2-5-1-ArrayList"><a href="#2-5-1-ArrayList" class="headerlink" title="2.5.1 ArrayList"></a>2.5.1 ArrayList</h3><p><code>java.util.ArrayList</code> 中重写了 祖先类 Object 的 clone() 方法，但本质上还是浅克隆</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> java.util;<br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ArrayList</span>&lt;E&gt; <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AbstractList</span>&lt;E&gt;<br>        <span class="hljs-keyword">implements</span> <span class="hljs-title class_">List</span>&lt;E&gt;, RandomAccess, Cloneable, java.io.Serializable<br>&#123;<br>   <span class="hljs-keyword">public</span> Object <span class="hljs-title function_">clone</span><span class="hljs-params">()</span> &#123;<br>        <span class="hljs-keyword">try</span> &#123;<br>            <span class="hljs-comment">// 1. 直接克隆整个列表</span><br>            ArrayList&lt;?&gt; v = (ArrayList&lt;?&gt;) <span class="hljs-built_in">super</span>.clone();<br>            <span class="hljs-comment">// 2. 调用 Ararys.copyof 克隆列表的每个元素</span><br>            v.elementData = Arrays.copyOf(elementData, size);<br>            <span class="hljs-comment">// 3. 初始化modCount, 表示列表在结构上被修改的次数</span><br>            v.modCount = <span class="hljs-number">0</span>;<br>            <span class="hljs-keyword">return</span> v;<br>        &#125; <span class="hljs-keyword">catch</span> (CloneNotSupportedException e) &#123;<br>            <span class="hljs-comment">// this shouldn&#x27;t happen, since we are Cloneable</span><br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InternalError</span>(e);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="2-5-2-基于字节流处理的深克隆"><a href="#2-5-2-基于字节流处理的深克隆" class="headerlink" title="2.5.2 基于字节流处理的深克隆"></a>2.5.2 基于字节流处理的深克隆</h3><p>ArrayList 虽然重写了 clone方法，但是依然无法克隆列表里的引用类型，这里推荐使用下面的流处理方式来实现深克隆，使用这种方式要求列表里的元素都实现 Serializable  接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 1. 创建字节数组输出流</span><br><span class="hljs-type">ByteArrayOutputStream</span> <span class="hljs-variable">bos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayOutputStream</span>();<br><span class="hljs-comment">// 2. 转化为对象输出流</span><br><span class="hljs-type">ObjectOutputStream</span> <span class="hljs-variable">oos</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectOutputStream</span>(bos);<br><span class="hljs-comment">// 3. 将深克隆的对象写入到对象输出流</span><br>oos.writeObject(src);<br><span class="hljs-comment">// 4. 将 字节数组输出流 读取为 字节数组输入流</span><br><span class="hljs-type">ByteArrayInputStream</span> <span class="hljs-variable">bai</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ByteArrayInputStream</span>(bos.toByteArray());<br><span class="hljs-comment">// 5. 转化为对象输入流</span><br><span class="hljs-type">ObjectInputStream</span> <span class="hljs-variable">ois</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">ObjectInputStream</span>(bai);<br><span class="hljs-comment">// 6. 读取对象</span><br><span class="hljs-keyword">return</span> (List) ois.readObject();<br></code></pre></td></tr></table></figure>
<h1 id="三、总结"><a href="#三、总结" class="headerlink" title="三、总结"></a>三、总结</h1><hr>
<p>工厂方法设计模式 logback<br>抽象工厂模式 Spring<br>建造者模式 StringBuilder、Mybatis<br>单例模式 Runtime、Integer、Spring<br>原型模式 Object.clone() ，</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" class="category-chain-item">设计模式</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">#设计模式</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>设计模式 笔记11 _ 创建型设计模式在源码中的应用 总结篇（一）</div>
      <div>http://example.com/2022/07/29/设计模式 笔记11 _ 创建型设计模式在源码中的应用 总结篇（一）/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月29日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/08/03/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%984_%20%E5%9F%BA%E4%BA%8E%20SpringBoot%20%E7%9A%84%E8%B6%85%E5%B8%82%E8%B4%A6%E5%8D%95%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/" title="项目实战4_ 基于 SpringBoot 的超市账单管理系统">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">项目实战4_ 基于 SpringBoot 的超市账单管理系统</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/07/29/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%20%E7%AC%94%E8%AE%B03%20_%20%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F%20%E5%9C%A8%E6%BA%90%E7%A0%81%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8%20Runtime%20%E3%80%81Integer%E3%80%81Spring%20@Bean/" title="设计模式 笔记3 _ 单例模式 在源码中的应用 Runtime 、Integer、Spring @Bean">
                        <span class="hidden-mobile">设计模式 笔记3 _ 单例模式 在源码中的应用 Runtime 、Integer、Spring @Bean</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
